package com.slm.report.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.slm.mylibrary.base.BaseFragment;
import com.slm.report.adapter.AddStudentAdapter;
import com.slm.report.databinding.FragmentStudentInfoBinding;
import com.slm.report.db.ContentDataBean;
import com.slm.report.db.ContentType;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.state.StudentInfoViewModel;
import com.slm.report.ui.AddStudentInfoActivity;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui.fragment
 * @ClassName: StudentInfoFragment
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/2 22:31
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/2 22:31
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class StudentInfoFragment extends BaseFragment<StudentInfoViewModel> {
    private FragmentStudentInfoBinding viewBinding;
    private List<ContentDataBean> contentDataBeans = new ArrayList<>();
    private String[] baseTitles = new String[]{"姓名", "性别", "年龄", "填表日期", "学校", "年级", "班级", "联系电话", "身高(cm)", "会员开始时间",
            "会员结束时间"};
    private ContentType[] baseTypes = new ContentType[]{ContentType.SINGLE_LINE_TEXT, ContentType.RADIO
            , ContentType.CHOICE_TV, ContentType.TIME_TV, ContentType.SCHOOL_CHOICE_TV, ContentType.CHOICE_TV, ContentType.SINGLE_LINE_TEXT, ContentType.SINGLE_LINE_TEXT
            , ContentType.SINGLE_LINE_TEXT, ContentType.TIME_TV, ContentType.TIME_TV};
    private String[] visionTitles = new String[]{"裸眼视力", "复查裸眼视力", "带镜史"};
    private ContentType[] visionTypes = new ContentType[]{ContentType.VISION_TEXT, ContentType.VISION_TEXT, ContentType.HIS_VISION_TEXT};
    private String[] qgTitles = new String[]{"球镜", "柱镜", "轴度"};
    private ContentType[] qgTypes = new ContentType[]{ContentType.VISION_TEXT, ContentType.VISION_TEXT
            , ContentType.VISION_TEXT};

    private StudentInfoBean studentInfoBean;
    private AddStudentAdapter studentAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentStudentInfoBinding.inflate(LayoutInflater.from(getContext()));
        //基本信息
        for (int i = 0; i < baseTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("基本信息");
            dataBean.setTitleStr(baseTitles[i]);
            dataBean.setType(baseTypes[i]);
            contentDataBeans.add(dataBean);
        }

        for (int i = 0; i < visionTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("视力情况");
            dataBean.setTitleStr(visionTitles[i]);
            dataBean.setType(visionTypes[i]);
            contentDataBeans.add(dataBean);
        }


        for (int i = 0; i < qgTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("屈光度检查");
            dataBean.setTitleStr(qgTitles[i]);
            dataBean.setType(qgTypes[i]);
            contentDataBeans.add(dataBean);
        }

       studentAdapter = new AddStudentAdapter(getContext(), contentDataBeans, false);
        viewBinding.listView.setAdapter(studentAdapter);
        mViewModel.studentInfoBean.observe(this, new Observer<StudentInfoBean>() {
            @Override
            public void onChanged(StudentInfoBean infoBean) {
                studentInfoBean = infoBean;
                showData();
            }
        });

        viewBinding.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AddStudentInfoActivity.class);
                intent.putExtra("data", studentInfoBean);
                startActivityForResult(intent, 101);
            }
        });

        return viewBinding.getRoot();
    }

    private void showData() {
        contentDataBeans.get(0).setContent(studentInfoBean.getName());
        contentDataBeans.get(1).setContent(studentInfoBean.getSex());
        contentDataBeans.get(2).setContent(studentInfoBean.getAge());
        contentDataBeans.get(3).setContent(studentInfoBean.getCreateTime());
        contentDataBeans.get(4).setContent(studentInfoBean.getSchoolName());
        contentDataBeans.get(5).setContent(studentInfoBean.getNianji());
        contentDataBeans.get(6).setContent(studentInfoBean.getBanName());
        contentDataBeans.get(7).setContent(studentInfoBean.getPhone());
        contentDataBeans.get(8).setContent(studentInfoBean.getHeight());
        contentDataBeans.get(9).setContent(studentInfoBean.getVipStartTime());
        contentDataBeans.get(10).setContent(studentInfoBean.getVipEndTime());
        contentDataBeans.get(11).getValueMap().put("left", studentInfoBean.getLuoLeftVal());
        contentDataBeans.get(11).getValueMap().put("right", studentInfoBean.getLuoRightVal());
        contentDataBeans.get(12).getValueMap().put("left", studentInfoBean.getFuLeftVal());
        contentDataBeans.get(12).getValueMap().put("right", studentInfoBean.getFuRightVal());

        contentDataBeans.get(13).getValueMap().put("key", studentInfoBean.getIsHaveHis());
        contentDataBeans.get(13).getValueMap().put("left", studentInfoBean.getHisLeftVal());
        contentDataBeans.get(13).getValueMap().put("right", studentInfoBean.getHisRightVal());


        contentDataBeans.get(14).getValueMap().put("left", studentInfoBean.getQiuLeftVal());
        contentDataBeans.get(14).getValueMap().put("right", studentInfoBean.getQiuRightVal());
        contentDataBeans.get(15).getValueMap().put("left", studentInfoBean.getZhuLeftVal());
        contentDataBeans.get(15).getValueMap().put("right", studentInfoBean.getZhuRightVal());
        contentDataBeans.get(16).getValueMap().put("left", studentInfoBean.getZhouLeftVal());
        contentDataBeans.get(16).getValueMap().put("right", studentInfoBean.getZhouRightVal());

        studentAdapter.notifyDataSetChanged();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK && requestCode == 101 && data != null) {
            if (data.getSerializableExtra("data") != null) {
                mViewModel.isChangeData.setValue(true);
                studentInfoBean = (StudentInfoBean) data.getSerializableExtra("data");
                showData();
            }
        }
    }
}
